package ar.edu.taxi.persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class DAOTrips extends DataAccesObject implements IfDAO{
	
	@Override
	public int delete(String condicion) {
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "DELETE FROM trips " +
						"WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

	@Override
	public IfDTO find(String codigo) {
		DTOTrips trip = null;
		Connection con = getConexion();
		if(codigo == null || codigo.length() == 0) return null;
		String query = "SELECT * FROM trips " +
						"WHERE " + codigo;
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				trip = new DTOTrips(rs.getInt("id"),
						rs.getTime("reserveDate"), 
						rs.getTime("tripDate"),
						rs.getTime("startDate"),
						rs.getTime("finishDate"),
						rs.getDouble("fromLat"),
						rs.getDouble("fromLon"),
						rs.getDouble("toLat"),
						rs.getDouble("toLon"),
						rs.getInt("idDriver"),
						rs.getInt("idVehicle"),
						rs.getInt("idPassanger"),
						rs.getInt("idState"));
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return trip;
	}

	@Override
	public int insert(IfDTO bean) {
		int numFilas = 0;
		DTOTrips trip = (DTOTrips) bean;
		Connection con = getConexion();
		String query = "INSERT INTO trips VALUES( DEFAULT"+
						", '"+trip.getReserveDateToString() + "'" +
						", '"+trip.getTripToString() + "'" +
						", '"+trip.getStartDateToString() + "'" +
						", '"+trip.getFinishDateToString() + "'" +
						", '"+trip.getFromLat() + "'" +
						", '"+trip.getFromLon() + "'" +
						", '"+trip.getToLat() + "'" +
						", '"+trip.getToLon() + "'" +
						", '"+trip.getIdDriver() + "'" +
						", '"+trip.getIdVehicle() + "'" +
						", '"+trip.getIdPassanger() + "'" +
						", '"+trip.getIdState() + "'" +
						" )";
		try {
			Statement sentencia = con.createStatement();
			numFilas = sentencia.executeUpdate(query);
			sentencia.close();
			cerrarConexion( con );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return numFilas;
	}

	@Override
	public Vector<IfDTO> select(String condicion) {
		Vector<IfDTO> trips = new Vector<IfDTO>();
		Connection con = getConexion();
		String query = "SELECT * FROM trips " +
						(condicion == null || condicion.length() == 0 ? "" : "WHERE " + condicion) +
						" ORDER BY id";
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				DTOTrips trip = new DTOTrips(rs.getInt("id"),
						rs.getTime("reserveDate"),
						rs.getTime("tripDate"),
						rs.getTime("startDate"),
						rs.getTime("finishDate"),
						rs.getDouble("fromLat"),
						rs.getDouble("fromLon"),
						rs.getDouble("toLat"),
						rs.getDouble("toLon"),
						rs.getInt("idDriver"),
						rs.getInt("idVehicle"),
						rs.getInt("idPassanger"),
						rs.getInt("idState"));
				trips.add(trip);
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return trips;
	}

	@Override
	public int update(IfDTO dto, String condicion) {
		DTOTrips trip = (DTOTrips) dto;
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "UPDATE trips SET" +
						" reserveDate='" + trip.getReserveDateToString() + "'" +
						" , tripDate='" + trip.getTripToString() + "'" +
						" , startDate='" + trip.getStartDateToString() + "'" +
						" , finishDate='" + trip.getFinishDateToString() + "'" +
						" , fromLat='" + trip.getFromLat() + "'" +
						" , fromLon='" + trip.getFromLon() + "'" +
						" , toLat='" + trip.getToLat() + "'" +
						" , toLon='" + trip.getToLon() + "'" +
						" , idDriver='" + trip.getIdDriver() + "'" +
						" , idVehicle='" + trip.getIdVehicle() + "'" +
						" , idPassanger='" + trip.getIdPassanger() + "'" +
						" , idState='" + trip.getIdState() + "'" +
						" WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

}
